<?php


namespace app\project\admin;


use app\admin\controller\Admin;
use app\project\model\Config as ModelConfig;
use app\common\builder\ZBuilder;

class Config extends Admin
{
    public function initialize()
    {
        parent::initialize();
        $this->model = new ModelConfig();
    }
    public function index()
    {
        $data = $this->model->select();

        $btn_dels = [
                'title'       => '删除',
                'icon'        => 'fa fa-times-circle-o',
                'class'       => 'btn btn-danger ajax-post confirm',
                'target-form' => 'ids',
                'href'  => url('del'),
                'data-title'=>'真的要删除吗?'
            ];

        return ZBuilder::make('table')
            ->hideCheckbox()
            ->addColumns([
                ['title', '配置标题'],
                ['key', '配置名称'],
                ['value', '配置值'],
                ['right_button', '操作', 'btn']
            ])
            ->addTopButton('add', [],true)
//            ->addTopButton('del',$btn_dels)
            ->addRightButton('delete')
            ->setRowList($data)
            ->fetch();
    }

    public function add()
    {
        if ($this->request->isPost()) {
            $data = $this->request->param('', null, 'trim');
            $result = $this->validate($data, 'Config');
            if (true !== $result) {
                $this->error($result);
            }
            if ($info = $this->model->create($data)){
                $details = UID . '操作config_add,id为' . $info['id'];
                action_log('config_add', 'config', $info['id'], UID, $details);
                $this->success('新增成功','index',['_parent_reload'=>1,'_close_pop'=>1]);
            }
            $this->error('新增失败');
        }
        return ZBuilder::make('form')
            ->addFormItems([
                ['text','title', '配置标题', '一般由中文组成，仅用于显示'],
                ['text','key', '配置名称', '由英文字母和下划线组成，如 <code>web_site_title</code>，调用方法：<code>config(\'web_site_title\')</code>'],
                ['number','value', '配置值', '该配置为百分比,如设置20%,请输入20'],
            ])
            ->fetch();
    }
}